package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.UserDto;
import com.zzyl.entity.User;
import com.zzyl.entity.UserRole;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.mapper.UsersMapper;
import com.zzyl.properties.SecurityConfigProperties;
import com.zzyl.service.UserService;
import com.zzyl.utils.NoProcessing;
import com.zzyl.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class UserServiceimpl implements UserService {
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private SecurityConfigProperties securityConfigProperties;

    @Override
    public PageResponse<UserVo> findUserPage(UserDto userDto, int pageNum, int pageSize) {
        //构建分页对象
        PageHelper.startPage(pageNum, pageSize);
        userDto.setDeptNo(NoProcessing.processString(userDto.getDeptNo()));
        Page<List<UserVo>> page = usersMapper.getPage(userDto);
        return PageResponse.of(page,UserVo.class);
    }

    /**
     * 用户添加
     * @param userDto
     */
    @Override
    public void addUser(UserDto userDto) {
        User user = new User();
        BeanUtil.copyProperties(userDto, user);
        user.setUsername(user.getEmail());
        user.setNickName(user.getRealName());
        user.setDataState(SuperConstant.DATA_STATE_0);
        String password = "itcast";
        user.setPassword(BCrypt.hashpw(password, BCrypt.gensalt()));
        usersMapper.insert(user);

        Set<String> roleVoIds = userDto.getRoleVoIds();
        ArrayList<UserRole> userRoles = new ArrayList<>();
        for (String roleVoId : roleVoIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(Long.parseLong(roleVoId));
            userRole.setDataState(SuperConstant.DATA_STATE_0);
            userRoles.add(userRole);
        }
        userRoleMapper.batchInsert(userRoles);
    }

    /**
     * 用户修改
     * @param userDto
     */
    @Override
    public void updateUser(UserDto userDto) {
        User user = new User();
        BeanUtil.copyProperties(userDto, user);
        user.setUpdateTime(LocalDateTime.now());
        user.setUpdateBy(1L);
        usersMapper.updateUser(user);

        userRoleMapper.deleteUserRoleByUserId(user.getId());
        Set<String> roleVoIds = userDto.getRoleVoIds();
        ArrayList<UserRole> userRoles = new ArrayList<>();
        for (String roleVoId : roleVoIds) {
            UserRole userRole = new UserRole();
            userRole.setUserId(user.getId());
            userRole.setRoleId(Long.parseLong(roleVoId));
            userRole.setDataState(SuperConstant.DATA_STATE_0);
            userRoles.add(userRole);
        }
        userRoleMapper.batchInsert(userRoles);
    }

    /**
     * 用户删除
     * @param userId
     */
    @Override
    public void deleteUser(Long userId) {
        usersMapper.deleteUser(userId);
        userRoleMapper.deleteUserRoleByUserId(userId);
    }

    /**
     * 用户启用禁用
     * @param id
     * @param status
     */
    @Override
    public void isEnable(Long id, String status) {
       User user = usersMapper.select(id);
       user.setDataState(status);
        usersMapper.updateEnable(user);
    }

    /**
     * 重置密码
     * @param userId
     */
    @Override
    public void resetPasswords(Long userId) {
        // 从配置中获取默认密码，并使用BCrypt进行哈希处理，生成安全密码
        String password = BCrypt.hashpw(securityConfigProperties.getDefaulePassword(), BCrypt.gensalt());
        // 构建一个User对象，并设置密码为刚才生成的安全密码
        User user = new User();
        user.setPassword(password);
        // 设置用户ID
        user.setId(userId);
        // 更新用户信息到数据库
        usersMapper.updateUser(user);
    }



}
